<?php
	$cid = $_SESSION['cid'];
	$aid = $_SESSION['uid'];

	//mysql_query("DELETE FROM hospital_log");

	if(getCharCurrHp($aid) <= 0 && isset($_SESSION['uid']))
	{
		setHospital($cid);
	}

	$q = mysql_query("SELECT * FROM hospital_log WHERE cid='$cid'");
	if(mysql_num_rows($q) == 1)
	{
		$maxhp = round(getCharMaxHp($aid)/2);

		$f = mysql_fetch_array($q);
		$time = time();

		if($time >= $f['endtime'])
		{
			mysql_query("UPDATE characters SET hp='$maxhp' WHERE id='$cid'");
			mysql_query("DELETE FROM hospital_log WHERE endtime < '$time' AND cid='$cid'");
		}
	}


	function checkHospital()
	{
		$cid = $_SESSION['cid'];
		$time = time();
		$q = mysql_query("SELECT * FROM hospital_log WHERE cid='$cid' AND endtime > '$time'");
		if(mysql_num_rows($q) == 1)
		{
			alert("You are in the hospital.");
			redirect("?page=hospital");
		}
	}

	function setHospital($cid)
	{
		$time = time();
		$time = $time+(60*20);
		mysql_query("INSERT INTO hospital_log (cid, endtime) VALUES ('$cid', '$time')")or die(alert(mysql_error()));
		mysql_query("UPDATE characters SET hp='1' WHERE id='$cid'");
		alert("You ended up in an hospital!");
		redirect("?page=hospital");
		die();
	}
?>